package org.anddev.andengine.engine.handler.bounds;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.shape.IShape;


	public class OutOfBoundsHandler implements IUpdateHandler {
	        // ===========================================================
	        // Constants
	        // ===========================================================
	 
	        // ===========================================================
	        // Fields
	        // ===========================================================
	 
	        private final IOutOfBoundsCallback mOutOfBoundsCallback;
	        private final IShape mShape;
	        private final int mWidth;
	        private final int mHeight;
	 
	        // ===========================================================
	        // Constructors
	        // ===========================================================
	 
	        /**
	         * This handler is used to determine if an IShape-object is within
	         * the given boundaries. Usage of this handler could be bullets that
	         * are off screen and need to be "cleaned up".
	         */
	        public OutOfBoundsHandler(final IOutOfBoundsCallback pOutOfBoundsCallback,
	                        final IShape pShape,
	                        final Integer pWidth,
	                        final Integer pHeight) throws IllegalArgumentException
	        {
	                if (pOutOfBoundsCallback == null) {
	                        throw new IllegalArgumentException( "Callback must not be null!");
	                }
	                if (pShape == null)
	                {
	                        throw new IllegalArgumentException( "pShape must not be null!");
	                }
	                if(pWidth == null)
	                {
	                        throw new IllegalArgumentException( "pWidth must not be null!");       
	                }
	                if(pHeight == null)
	                {
	                        throw new IllegalArgumentException( "pHeight must not be null!");      
	                }
	 
	                this.mOutOfBoundsCallback = pOutOfBoundsCallback;
	                this.mShape = pShape;
	                this.mWidth = pWidth.intValue();
	                this.mHeight = pHeight.intValue();
	        }
	 
	        // ===========================================================
	        // Getter & Setter
	        // ===========================================================
	 
	        // ===========================================================
	        // Methods for/from SuperClass/Interfaces
	        // ===========================================================
	 
	        @Override
	        public void onUpdate(final float pSecondsElapsed)
	        {
	               
	                if(mShape.getX() > mWidth)
	                {
	                        final boolean proceed = this.mOutOfBoundsCallback.onOutOfBounds(mShape, IOutOfBoundsCallback.BOUND_RIGHT);
	                        if(!proceed)
	                        {
	                                return;
	                        }
	                }
	                if (mShape.getX() < 0)
	                {
	                        final boolean proceed = this.mOutOfBoundsCallback.onOutOfBounds(mShape, IOutOfBoundsCallback.BOUND_LEFT);
	                        if(!proceed)
	                        {
	                                return;
	                        }
	                }
	                if(mShape.getY() > mHeight)
	                {
	                        final boolean proceed = this.mOutOfBoundsCallback.onOutOfBounds(mShape, IOutOfBoundsCallback.BOUND_BOTTOM);
	                        if(!proceed)
	                        {
	                                return;
	                        }
	                }
	                if (mShape.getY() < 0)
	                {
	                        final boolean proceed = this.mOutOfBoundsCallback.onOutOfBounds(mShape, IOutOfBoundsCallback.BOUND_TOP);
	                        if(!proceed)
	                        {
	                                return;
	                        }
	                }
	        }
	 
	        @Override
	        public void reset() {
	 
	        }
	 
	        // ===========================================================
	        // Methods
	        // ===========================================================
	 
	        // ===========================================================
	        // Inner and Anonymous Classes
	        // ===========================================================
	}